package com.cal.utms.action.maintenance;

import java.util.ArrayList;
import java.util.List;

import com.cal.utms.dto.maintenance.InstrumentTypeDto;
import com.cal.utms.entity.Instrument;
import com.cal.utms.entity.InstrumentType;
import com.cal.utms.dao.dealing.InstrumentDao;
import com.cal.utms.dao.maintenance.InstrumentTypeDao;
import com.cal.utms.model.maintenance.InstrumentTypeModel;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class InstrumentTypeAction extends ActionSupport implements ModelDriven<InstrumentTypeDto> {


	private static final long serialVersionUID = 1L;
	private InstrumentTypeDto instrumentTypeDto = new InstrumentTypeDto();
		
	public InstrumentTypeDto getInstrumentTypeBean() {
		return instrumentTypeDto;
	}

	public void setInstrumentTypeBean(InstrumentTypeDto instrumentTypeDto) {
		this.instrumentTypeDto = instrumentTypeDto;
	}

/*	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}*/

	@Override
	public InstrumentTypeDto getModel() {
		return instrumentTypeDto;
	}
	
	public String saveInstrumentType() {
		InstrumentTypeDao dao = new InstrumentTypeDao();
		System.out.println("Saving "+instrumentTypeDto);
		InstrumentTypeModel model = new InstrumentTypeModel();
		InstrumentType instrumentType = null;
		try{
			instrumentType = model.createEntity(instrumentTypeDto);
		}catch(NullPointerException npe){
			instrumentTypeDto.setSuccess(false);
			return Action.ERROR;
		}
		Object ob = dao.addInstrumentType(instrumentType);
		if(ob instanceof Integer){
			instrumentTypeDto.setSuccess(true);
			instrumentTypeDto.setId((Integer) ob);
			return Action.SUCCESS;
		}else{
			instrumentTypeDto.setSuccess(false);
			return Action.ERROR;
		}
	}
	
	public String updateInstrumentType(){
		InstrumentTypeDao instrumentTypeDao = new InstrumentTypeDao();
		InstrumentTypeModel insTypeModel = new InstrumentTypeModel();
		InstrumentType instrumentType = null;
		
		instrumentTypeDto.setSuccess(false);
		instrumentType = instrumentTypeDao.getInstrumentTypeByName(instrumentTypeDto.getInstrumentName());		
		try{
			insTypeModel.updateEntity(instrumentTypeDto, instrumentType);
		}catch(NullPointerException npe){
			System.out.println("Error: Update InsType:"+instrumentType.getId());
			npe.printStackTrace();
			return Action.ERROR;
		}
		instrumentTypeDao.updateInstrumentType(instrumentType);
		instrumentTypeDto.setSuccess(true);
		return Action.SUCCESS;
	}
	
	public String getInstrumentTypeList(){
		InstrumentTypeDao dao = new InstrumentTypeDao();
		InstrumentTypeModel instrumentTypeModel = new InstrumentTypeModel();
		System.out.println("Getting all InstrumentTypes - querry:"+instrumentTypeDto.getQuerry());
		List<InstrumentType> insTypeList;
		List<InstrumentTypeDto> beanList = new ArrayList<InstrumentTypeDto>();
		if(instrumentTypeDto.getQuerry()!=null && !instrumentTypeDto.getQuerry().equals("")){
			insTypeList = dao.getInstrumentTypeList(instrumentTypeDto.getQuerry());
		}else{
			insTypeList = dao.getInstrumentTypeList();
		}		
		if(insTypeList!=null)
		for(InstrumentType instrumentType:insTypeList){
			beanList.add(instrumentTypeModel.decodeEntity(instrumentType));
		}
		instrumentTypeDto.setInstrumentTypeList(beanList);
		instrumentTypeDto.setSuccess(true);
		return Action.SUCCESS;
		
	}
	
	public String checkInstrumentCode(){
		if(instrumentTypeDto.getQuerry()!=null){
			InstrumentDao insDao = new InstrumentDao();
			Instrument instrument = insDao.getByCode(instrumentTypeDto.getQuerry());
			if(instrument == null){
				instrumentTypeDto.setSuccess(false);
			}else{
				instrumentTypeDto.setSuccess(true);
			}
		}else{
			instrumentTypeDto.setSuccess(false);
		}
		return Action.SUCCESS;
	}
	
	public String checkInstrumentTypeCode(){
		if(instrumentTypeDto.getQuerry()!=null){
			InstrumentTypeDao insTypeDao = new InstrumentTypeDao();
			InstrumentType insType = insTypeDao.getInstrumentTypeByName(instrumentTypeDto.getQuerry());
			if(insType == null){
				instrumentTypeDto.setSuccess(false);
			}else{
				instrumentTypeDto.setSuccess(true);
			}
		}else{
			instrumentTypeDto.setSuccess(false);
		}
		return Action.SUCCESS;
	}

}
